package com.iman.locker;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.iman.dao.User;
import com.iman.locker.view.LockActivity;


/**
 * 应用前后台切换监听和处理
 * Created by esirong on 2015/12/4.
 */
public class Foreground implements Application.ActivityLifecycleCallbacks {
    private static final String TAG = "Foreground";
    private static final long CHECK_DELAY = 500;
    private static Foreground instance;
    private boolean foreground = false;
    private boolean paused = true;
    private Handler handler = new Handler();
    private Runnable check;

    private Foreground() {
    }

    public static void init(Application application) {
        if (instance == null) {
            instance = new Foreground();
            application.registerActivityLifecycleCallbacks(instance);
        }
    }

    public static Foreground get() {
        return instance;
    }

    public boolean isForeground() {
        return foreground;
    }

    public boolean isBackground() {
        return !foreground;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {
        paused = false;
        if (isBackground() && isAPasswordWasSet()) {
            //如果是从后台到前台的，并且设置了密码的，提示锁屏
            LockActivity.showLocker(activity, null);
        }
        foreground = true;
        if (check != null) {
            handler.removeCallbacks(check);
        }
    }

    public boolean isAPasswordWasSet() {
        User user = MyApp.getInstance().getUser();
        if (user == null) {
            return false;
        }
        if (user.getLockType() == null) {
            return false;
        }
        if (TextUtils.isEmpty(user.getPasswordPin()) && TextUtils.isEmpty(user.getPasswordDraw())) {
            return false;
        }
        return true;
    }

    private int showLockerView() {
        User user = MyApp.getInstance().getUser();
        int lockType = -1;
        if (user.getLockType() != null) {
            lockType = user.getLockType();
        } else {
            lockType = Constant.LOCK_TYPE_DRAW;
        }

        return lockType;
    }

    @Override
    public void onActivityPaused(Activity activity) {
        paused = true;
        if (check != null) {
            handler.removeCallbacks(check);
        }
        handler.postDelayed(check = new Runnable() {
            @Override
            public void run() {
                if (foreground && paused) {
                    foreground = false;
                    Log.d(TAG, "App went background");
                } else {
                    Log.d(TAG, "App still foreground");
                }
            }
        }, CHECK_DELAY);
    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }
}
